package com.ali.ttt.webSocket.cluster;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @Author : yuxl
 * @create 2024/7/15 1:14 PM
 */
@Configuration
public class RedisSubscriberConfig {

    @Resource
    private NettyRedisMessageListener nettyRedisMessageListener;

    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory redisConnectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(redisConnectionFactory);
        //订阅频道
        List<PatternTopic> topicList = Arrays.asList(new PatternTopic("netty.*"));
        container.addMessageListener(nettyRedisMessageListener, topicList);
        return container;
    }
}

